package com.css.fxfzxczh.modules.synthesizeInfoManage.houseStructureManagement.repository.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.synthesizeInfoManage.houseStructureManagement.entity.HouseStructureManagement;
import com.css.fxfzxczh.modules.synthesizeInfoManage.houseStructureManagement.repository.HouseStructureManagementRepository;
import com.css.fxfzxczh.modules.synthesizeInfoManage.knowledgeManagement.entity.KnowledgeManagement;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/01/12
 * @Description: 房屋结构类型管理
 */
@Repository
public class HouseStructureManagementRepositoryImpl implements HouseStructureManagementRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static final String TABLE_NAME = "xczh_house_structure_type_management"; // 表名
    private static final String TABLE_ID = "xhstm_id"; // 表id字段名

    @Override
    public void save(HouseStructureManagement param) {
        String sql = "insert into " + TABLE_NAME +
                " (xhstm_id,structure_type,comment_info," +
                "category,company,del_flag,create_user,create_time)" +
                " values(:xhstmId,:structureType,:commentInfo," +
                ":category,:company,:delFlag,:createUser,:createTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(param);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void updateById(HouseStructureManagement param) {
        String sql = "update " + TABLE_NAME +
                " set structure_type = :structureType,comment_info = :commentInfo," +
                "category = :category,update_user=:updateUser,update_time=:updateTime where del_flag = '0' and " + TABLE_ID + " = '" + param.getXhstmId() + "'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(param);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void deleteById(String id) {
        String sql = "update " + TABLE_NAME + " set del_flag = ?  where " + TABLE_ID + " = ?";
        jdbcTemplate.update(sql, YNEnum.Y.toString(), id);
    }

    @Override
    public Map<String, Object> getPage(HouseStructureManagement queParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from " + TABLE_NAME);
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by create_time asc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        //房屋结构类型
        if (!PlatformObjectUtils.isEmpty(queParams.getStructureType())) {
            whereSql.append(" and structure_type like '%" + queParams.getStructureType() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(queParams.getCategory())) {
            whereSql.append(" and category = '" + queParams.getCategory() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(queParams.getPersonName())) {
            whereSql.append(" and category like '%" + queParams.getPersonName() + "%'");
        }
        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(HouseStructureManagement.class)).size();
        List<HouseStructureManagement> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(HouseStructureManagement.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public HouseStructureManagement findById(String param) {
        String sql = "select * from " + TABLE_NAME + " where " + TABLE_ID + " = '" + param + "'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(HouseStructureManagement.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<HouseStructureManagement> findAll() {
        String sql ="select * from "+TABLE_NAME+" where del_flag = '0' ";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HouseStructureManagement.class));
    }

    @Override
    public List<String> getPreStrType(String category) {
        String sql ="select structure_type from "+TABLE_NAME+" where category = '"+category+"' and  del_flag = '0'  order by structure_type ";
        return jdbcTemplate.queryForList(sql, String.class);
    }
}
